package com.gdwProject.tankgame4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author 关道伟
 * @version 1.0
 * 坦克大战的绘图区域
 */

//为了监听键盘事件  实现接口KeyLisenter
public class MyPanel extends JPanel implements KeyListener,Runnable {
    MyTank myTank = null;
    //定义敌方坦克
    Vector<EnermyTank> enermyTanks = new Vector<>();
    int enermyTankSize = 3;
    public MyPanel(){
        //初始化自己的坦克
        myTank = new MyTank(100,100);
        myTank.setSpeed(3);
        //初始化2敌人的坦克
        for (int i = 0; i < enermyTankSize; i++) {
            //创建敌人坦克对象
            EnermyTank enermyTank = new EnermyTank(100 * (i + 1), 0);
            //将敌人的坦克方向置为向下
            enermyTank.setDirect(2);
            //将坦克实体 放入集合当中
            enermyTanks.add(enermyTank);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1000,750);//填充矩形 默认黑色

        //画出自己的坦克 封装的方法
        drawTank(myTank.getX(),myTank.getY(),g,myTank.getDirect(),0);

        //画出自己的tank的子弹
        if(myTank.shot!=null&&myTank.shot.isLive!=false){//
            g.fill3DRect(myTank.shot.getX(),myTank.shot.getY(),4,4,false);
        }


        //画敌人的坦克 遍历vector
        for (int i = 0; i < enermyTanks.size(); i++) {
            EnermyTank enermyTank = enermyTanks.get(i);
            drawTank(enermyTank.getX(),enermyTank.getY(),g,enermyTank.getDirect(),1);
        }

    }


    //画出坦克
    public void drawTank(int x,int y,Graphics g,int direct,int type){
        //根据不同类型坦克 设置不同颜色
        switch(type){
            case 0://我们的坦克
                g.setColor(Color.green);
                break;
            case 1://敌人的tank'
                g.setColor(Color.red);
                break;
        }

        //根据坦克方向来绘制坦克
        //direct 表示方向  0：向上  1：向右 2：向下 3：向左
        switch (direct){
            case 0://表示向上
                g.fill3DRect(x,y,10,60,false);//坦克左轮
                g.fill3DRect(x+30,y,10,60,false);//坦克右轮
                g.fill3DRect(x+10,y+10,20,40,false);//坦克的盖子
                g.fillOval(x+10,y+20,20,20);//画出圆盖
                g.drawLine(x+20,y,x+20,y+30);//坦克炮管
                break;
            case 1:
                g.fill3DRect(x,y,60,10,false);//坦克左轮
                g.fill3DRect(x,y+30,60,10,false);//坦克右轮
                g.fill3DRect(x+10,y+10,40,20,false);//坦克的盖子
                g.fillOval(x+20,y+10,20,20);//画出圆盖
                g.drawLine(x+30,y+20,x+60,y+20);//坦克炮管
                break;
            case 2:
                g.fill3DRect(x,y,10,60,false);//坦克左轮
                g.fill3DRect(x+30,y,10,60,false);//坦克右轮
                g.fill3DRect(x+10,y+10,20,40,false);//坦克的盖子
                g.fillOval(x+10,y+20,20,20);//画出圆盖
                g.drawLine(x+20,y+30,x+20,y+60);//坦克炮管
                break;
            case 3:
                g.fill3DRect(x,y,60,10,false);//坦克左轮
                g.fill3DRect(x,y+30,60,10,false);//坦克右轮
                g.fill3DRect(x+10,y+10,40,20,false);//坦克的盖子
                g.fillOval(x+20,y+10,20,20);//画出圆盖
                g.drawLine(x+30,y+20,x ,y+20);//坦克炮管
                break;
            default:
                System.out.println("暂时没有处理");
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {// w a s d
        if(e.getKeyCode() == KeyEvent.VK_W){//按下了w建
            myTank.setDirect(0);
            //继续修改tank的坐标
            myTank.moveUp();
        }else if(e.getKeyCode() == KeyEvent.VK_D){
            myTank.setDirect(1);
            myTank.moveRight();
        }else if(e.getKeyCode() == KeyEvent.VK_S){
            myTank.setDirect(2);
            myTank.moveDown();
        }else if(e.getKeyCode() == KeyEvent.VK_A){
            myTank.setDirect(3);
            myTank.moveLeft();
        }

        //按下J发射子弹
        if(e.getKeyCode() == KeyEvent.VK_J){
            System.out.println("我方坦克发射子弹...");
            myTank.shotEnemyTank();
        }

        //面板重绘
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {//利用线程重绘
        while (true){
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }
    }
}
